package org.hhgzs.modules.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hhgzs.modules.erp.constants.BusinessConstants;
import org.hhgzs.modules.erp.entity.SystemConfig;
import org.hhgzs.modules.erp.mapper.SystemConfigMapper;
import org.hhgzs.modules.erp.service.SystemConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统参数 服务实现类
 * </p>
 *
 * @author peiqy
 * @since 2021-07-14
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SystemConfigServiceImpl extends ServiceImpl<SystemConfigMapper, SystemConfig> implements SystemConfigService {

    private final SystemConfigMapper systemConfigMapper;

    /**
     * 获取仓库开关
     *
     * @return
     * @throws Exception
     */
    @Override
    public boolean getDepotFlag() {
        boolean depotFlag = false;
        List<SystemConfig> list = getSystemConfig();
        if (list.size() > 0) {
            String flag = list.get(0).getDepotFlag();
            if (("1").equals(flag)) {
                depotFlag = true;
            }
        }
        return depotFlag;
    }

    /**
     * 获取客户开关
     *
     * @return
     * @throws Exception
     */
    @Override
    public boolean getCustomerFlag(){
        boolean customerFlag = false;
        List<SystemConfig> list = getSystemConfig();
        if (list.size() > 0) {
            String flag = list.get(0).getCustomerFlag();
            if (("1").equals(flag)) {
                customerFlag = true;
            }
        }
        return customerFlag;
    }

    /**
     * 获取负库存开关
     *
     * @return
     * @throws Exception
     */
    @Override
    public boolean getMinusStockFlag()  {
        boolean minusStockFlag = false;
        List<SystemConfig> list = getSystemConfig();
        if (list.size() > 0) {
            String flag = list.get(0).getMinusStockFlag();
            if (("1").equals(flag)) {
                minusStockFlag = true;
            }
        }
        return minusStockFlag;
    }

    public List<SystemConfig> getSystemConfig() {
        QueryWrapper<SystemConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SystemConfig::getDeleteFlag, BusinessConstants.DELETE_FLAG_DELETED);
        List<SystemConfig> list =  list(queryWrapper);
        return list;
    }
}
